<template>
  <MyDialog
    v-model="isVisible"
    :type="type"
    class="audit-dialog-wrapper"
    width="600"
    :dialog-title="dialogTitle"
    :need-modal="needModal"
    @closed="closed"
  >
    <my-form ref="ruleForm" v-model="dataForm" label-width="120px" :model="dataForm" :rules="rules" :is-grid="false" :items-datas="state.itemsDatas">
    </my-form>
    <template #footer>
      <my-button type="reset" title="关闭" @click="closed" />
      <my-button type="save" title="保存" :loading="buttonLoading" @click="submitForm" />
    </template>
  </MyDialog>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ab04Calculation } from '@/api/yuanshidian/ab04';
const { proxy } = getCurrentInstance();
const props = defineProps(
  {
    dialogTitle: {
      type: String,
      default: '弹窗标题'
    },
    needModal: {
      type: Boolean,
      default: true
    },
    modelValue: {
      type: Boolean,
      default: false
    },
    currentRow: {
      type: Object,
      default: () => {}
    }
  },
  ['modelValue']
);
const buttonLoading = ref(false);
const ruleForm = ref<ElFormInstance>();
const state = reactive({
  dataForm: {
    id: '',
    calculationType: '',
    jfStarttime: '',
    jfEndtime: ''
  },
  rules: {
    //   dw01: [{ required: true, message: '请输入', trigger: 'blur' }],
  },
  itemsDatas: [
    {
      label: '计息方法',
      prop: 'calculationType',
      type: 'radio',
      options: [
        { label: 1, name: '单利' },
        { label: 2, name: '复利' }
      ],
      span: 24
    },
    { label: '缴费开始时间', prop: 'jfStarttime', type: 'date', valueFormat: 'YYYY-MM-DD', span: 24 },
    { label: '缴费结束时间', prop: 'jfEndtime', type: 'date', valueFormat: 'YYYY-MM-DD', span: 24 }
  ]
});
const { dataForm, rules } = toRefs(state);
const isVisible = ref(false);
watch(
  () => props.modelValue,
  async (v) => {
    isVisible.value = v;
  },
  { immediate: true, deep: true }
);
const emit = defineEmits(['update:modelValue', 'handleQuery']);
const closed = () => {
  reset();
  emit('update:modelValue');
};
/** 表单重置 */
const reset = () => {
  ruleForm.value?.resetFields();
};

/** 提交按钮 */
const submitForm = () => {
  ruleForm.value?.validate(async (valid) => {
    if (valid) {
      buttonLoading.value = true;
      var params = {
        ...dataForm.value,
        id: props.currentRow.id
      };
      var res = await ab04Calculation(params).finally(() => (buttonLoading.value = false));
      if (res.code === 200) {
        proxy?.$modal.msgSuccess('操作成功');
        emit('handleQuery');
        closed();
      }
    }
  });
};
</script>
